<?php
namespace app\admin\controller;
use app\common\model;

class Menu extends Base
{   
    public $menuModel;
    public function __construct () {
        parent::__construct();
        $this->menuModel = new model\Menu;
    }

    /* 菜单列表
     *TODO 权限控制
     */
    public function list()
    {
        $where = [
            'status' => 1,
            'level' => 1
        ];
        if($this->manager['mall_id']==0) {
            $where['is_admin'] = 1;
        }else{
            $where['is_mall'] = 1;
        }
        $menus = $this->menuModel->getAll($where,'ord desc,id asc');
        foreach($menus as $k=>$v) {
            $subwhere = [
                'status' => 1,
                'level' => 2,
                'controller' => $v['controller']
            ];
            if($this->manager['mall_id']==0) {
                $subwhere['is_admin'] = 1;
                if($this->manager['is_super']==0) {
                    $subwhere['id'] = [
                        'in',explode(',',$this->manager['menu'])
                    ];
                }
            }else{
                $subwhere['is_mall'] = 1;
            }
            $subList = $this->menuModel->getAll($subwhere,'ord desc,id asc');
            if($subList) {
                foreach($subList as $kk=>$vv) {
                    $subList[$kk]['path'] = "pages/{$vv['controller']}/{$vv['action']}";
                }
                $menus[$k]['children'] = $subList;
            }else{
                unset($menus[$k]);
            }
        }
        foreach($menus as $v) {
            $new[] = $v;
        }
        return returnJson(0,'',$new);
    }
}
